<?php
namespace app\controller;

use app\BaseController;
use app\controller\model\Tables;
use liliuwei\think\Auth;
use EasyDingTalk\Application;
use think\facade\Db;
use think\facade\Session;
class Goods extends BaseController
{
    protected $gobaluser=0;
    protected $tables;
    public function __construct($api=0){
        if($api==0){
            checkuser();
        }
        $this->gobaluser=Session::get("uid");
        $this->tables=new Tables();
    }

    public function index()
    {
        return view()->assign(['cids'=>r_classes(214)]);
    }

    public function goodsAdd(){
        $id = request()->param("id");
        $id<1?$id=0:'';
        return view("goodsAdd")->assign(['cids'=>r_classes(214),'data'=>r_one($this->tables->goods,"id=$id")]);
    }
    public function goodsList($return=false){
        $cid = request()->param("cid");
        $cid>0?$where="cid=$cid":$where='1=1';
        $status = request()->param("status");
        $status==-1?$where.=" and status=-1":$where.=' and status=1';
        $cid = request()->param("cid");
        $cid>1?$where.=" and cid=$cid":'';
        $title = request()->param("title");
        $title!=''?$where.=" and title like '%$title%'":'';
        $res = getpagedata($this->tables->goods,"",true,$where);
        $count = $res[1];
        $res = $res[0];
        $citys = r_all("classes");
        $res=$res->all();
        foreach ($res as $k => $v){
            $res[$k]['cid'] = rclasstitle($citys,$v['cid']);
        }
        if($return){
            return $res;
        }
        pagejson($res,0,'',$count);
    }
    public function goodsSave(){
        $data = request()->param();
        unset($data['id']);
        $id = request()->param("id");
        !isset($data['title']) || $data['title']==''?echojson([],1,'标题必填'):'';
        !isset($data['content']) || $data['content']==''?echojson([],1,'内容不能为空'):'';
        $data['content'] = str_replace('"/uploads/','"'.apiURL().'/uploads/',$data['content']);
        $data['content'] = str_replace('<img src=','<img width=100%  src=',$data['content']);
        $data['content'] = str_replace('<img alt=','<img width=100%  alt=',$data['content']);
        $data['addtime']=date("Y-m-d H:i:s");
        if($id>0){
            $re = r_one($this->tables->goods,"id=$id");
            if($re){
                DB::name($this->tables->goods)->where("id",$id)->update($data);
                echojson([],0,'更新成功');
            }else{
                echojson([],1,'ID错误');
            }
        }else{
            $re = DB::name($this->tables->goods)->insert($data);
            if($re){
                echojson([],0,'添加成功');
            }else{
                echojson([],1,'ID错误');
            }
        }
    }


    public function banner()
    {
        return view();
    }
    public function addBanner(){
        $id = request()->param("id");
        $id<1?$id=0:'';
        return view("addBanner")->assign(['data'=>r_one("banner","id=$id"),'cids'=>r_classes(212)]);
    }
    public function bannerList(){
        $cid = request()->param("cid");
        $cid>0?$where="cid=$cid":$where='1=1';
        $status = request()->param("status");
        $status==-1?$where.=" and status=-1":$where.=' and status=1';
        $cid = request()->param("cid");
        $cid>1?$where.=" and cid=$cid":'';
        $title = request()->param("title");
        $title!=''?$where.=" and title like '%$title%'":'';
        getpagedata($this->tables->banner,"",false,$where);
    }
    public function bannerSave(){
        $data = request()->param();
        unset($data['id']);
        $id = request()->param("id");
        !isset($data['title']) || $data['title']==''?echojson([],1,'标题必填'):'';
        !isset($data['title2']) || $data['title2']==''?echojson([],1,'url链接必填'):'';
//        !isset($data['content']) || $data['content']==''?echojson([],1,'内容不能为空'):'';
        $data['addtime']=date("Y-m-d H:i:s");
        if($id>0){
            $re = r_one($this->tables->banner,"id=$id");
            if($re){
                DB::name($this->tables->banner)->where("id",$id)->update($data);
                echojson([],0,'更新成功');
            }else{
                echojson([],1,'ID错误');
            }
        }else{
            $re = DB::name($this->tables->banner)->insert($data);
            if($re){
                echojson([],0,'添加成功');
            }else{
                echojson([],1,'ID错误');
            }
        }
    }
}
